import sequelize from "../../config/db.js";
import {Sequelize} from "sequelize";


/**
 * @description 检查菜单是否已存在
 * @param {Object} menuData - 菜单数据
 * @returns {Promise<Array>} 返回现有菜单或空数组
 */
const existingMenu = async (menuData) => {
    const [existingMenu] = await sequelize.query(
        `SELECT *
         FROM sys_menu
         WHERE name = :name
           AND isDeleted = 1`,
        {
            replacements: {name: menuData.name}
        }
    );
    return existingMenu;
};

/**
 * @description 创建菜单
 * @param {Object} menuData - 菜单数据
 * @returns {Promise<void>}
 */
const createMenu = async (menuData) => {
    await sequelize.query(
        `INSERT INTO sys_menu (parent_id, menu_type, name, url, style, description, sort, isAccessible, isDeleted)
         VALUES (:parent_id, :menu_type, :name, :url, :style, :description, :sort, :isAccessible, 1)`,
        {
            replacements: {
                parent_id: menuData.parent_id,
                menu_type: menuData.menu_type,
                name: menuData.name,
                url: menuData.url,
                style: menuData.style,
                description: menuData.description,
                sort: menuData.sort || null,
                isAccessible: menuData.isAccessible !== undefined ? menuData.isAccessible : 1
            }
        }
    );
};

/**
 * @description 获取所有菜单
 * @returns {Promise<Array>} 返回菜单列表
 */
const getAllMenus = async () => {
    return await sequelize.query(
        `SELECT *
         FROM sys_menu
         WHERE isDeleted = 1`,
        {type: Sequelize.QueryTypes.SELECT}
    );
};

/**
 * @description 获取菜单信息
 * @param {number} menuId - 菜单 ID
 * @returns {Promise<Array>} 返回菜单信息或空数组
 */
const getMenuById = async (menuId) => {
    const [menu] = await sequelize.query(
        `SELECT *
         FROM sys_menu
         WHERE menu_id = :menuId`,
        {replacements: {menuId}}
    );
    return menu;
};

/**
 * @description 更新单个菜单
 * @param {number} menuId - 菜单 ID
 * @param {Object} menuData - 更新的数据
 * @returns {Promise<null|string>} 返回 null 或错误信息
 */
const updateMenu = async (menuId, menuData) => {
    await sequelize.query(
        `UPDATE sys_menu
         SET parent_id    = :parent_id,
             menu_type    = :menu_type,
             name         = :name,
             url          = :url,
             style        = :style,
             description  = :description,
             sort         = COALESCE(:sort, 1),         -- 如果 sort 未提供则默认值为 1
             isAccessible = COALESCE(:isAccessible, 1), -- 如果 isAccessible 未提供则默认值为 1
             isDeleted    = COALESCE(:isDeleted, 1)     -- 如果 isDeleted 未提供则默认值为 1
         WHERE menu_id = :menuId`,
        {
            replacements: {
                parent_id: menuData.parent_id || null,
                menu_type: menuData.menu_type,
                name: menuData.name,
                url: menuData.url,
                style: menuData.style,
                description: menuData.description,
                sort: menuData.sort,
                isAccessible: menuData.isAccessible,
                isDeleted: menuData.isDeleted,
                menuId
            }
        }
    );
    return null; // 返回 null 表示更新成功
};


/**
 * @description 逻辑删除菜单
 * @param {number} menuId - 菜单 ID
 * @returns {Promise<void>}
 */
const deleteMenu = async (menuId) => {
    await sequelize.query(
        `UPDATE sys_menu
         SET isDeleted = 0
         WHERE menu_id = :menuId`,
        {replacements: {menuId}}
    );
};

export default {existingMenu, createMenu, getAllMenus, getMenuById, updateMenu, deleteMenu};
